package cn.xfdzx.android.apps.shop.activity.myshop;


import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.remake.BigImageViewActivity;
import cn.xfdzx.android.apps.shop.adapter.ItemOrderXfdGoodsAdapter;
import cn.xfdzx.android.apps.shop.adapter.OrderXfdSaleAfterGoodsAdapter;
import cn.xfdzx.android.apps.shop.adapter.ShopInOrderAfterSalesImageAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.SalePiFaAfterDetailsBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.contract.SalePiFaAfterDetailsContract;
import cn.xfdzx.android.apps.shop.presenter.SalePiFaAfterDetailsPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.StatusBarUtil;
import cn.xfdzx.android.apps.shop.util.Utils;

public class SalePiFaAfterDetailsActivity extends BaseMvpActivity<SalePiFaAfterDetailsPresenter> implements SalePiFaAfterDetailsContract.View {

    @BindView(R.id.ll_state)
    LinearLayout llState;
    private RelativeLayout.LayoutParams item_bar;
    @BindView(R.id.iv_state)
    ImageView ivState;
    @BindView(R.id.tv_state)
    TextView tvState;
    @BindView(R.id.tv_sheng_time)
    TextView tvShengTime;

    @BindView(R.id.tv_why)
    TextView tvWhy;
    @BindView(R.id.tv_apply_time)
    TextView tvApplyTime;

    @BindView(R.id.tv_refund_money)
    TextView tvRefundMoney;
    @BindView(R.id.tv_sale_after_why)
    TextView tvSaleAfterWhy;
    @BindView(R.id.tv_refund_shuoming)
    TextView tvRefundShuoming;
    @BindView(R.id.rv_why_image)
    RecyclerView rvWhyImage;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.iv_user)
    ImageView ivUser;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_user_state)
    TextView tvUserState;
    @BindView(R.id.rv_goods)
    RecyclerView rvGoods;
    @BindView(R.id.tv_order_num)
    TextView tvOrderNum;
    @BindView(R.id.tv_order_copy)
    TextView tvOrderCopy;
    @BindView(R.id.tv_xiadan_time)
    TextView tvXiadanTime;
    @BindView(R.id.tv_pay_type)
    TextView tvPayType;
    @BindView(R.id.tv_peisong_type)
    TextView tvPeisongType;
    RoundedCorners roundedCorners;
    RequestOptions options;
    private String addressCopy = "";
    private String userPhone = "";
    private String orderNum = "";

    @Override
    public int getLayoutId() {
        return R.layout.activity_sale_pi_fa_after_details;
    }

    @Override
    public void initView() {
        initHigh();
        mPresenter = new SalePiFaAfterDetailsPresenter();
        mPresenter.attachView(this);
        mPresenter.initData(getIntent().getStringExtra("id"));
    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(this);
        }
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onInitDataSuccess(SalePiFaAfterDetailsBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            tvWhy.setVisibility(View.GONE);
            if (bean.getData().getStatus().getStatus() == 1) {//申请中
                tvShengTime.setTextColor(getResources().getColor(R.color.color_F93331));
                ivState.setImageResource(R.drawable.icon_sale_after_jinxing);
            } else if (bean.getData().getStatus().getStatus() == 3) {//拒绝驳回
                tvShengTime.setTextColor(getResources().getColor(R.color.color_F93331));
                ivState.setImageResource(R.drawable.icon_sale_after_bohui);
                tvWhy.setVisibility(View.VISIBLE);
                tvWhy.setText("驳回原因：" + bean.getData().getStatus().getTitle().getRefuse_reason());
            } else if (bean.getData().getStatus().getStatus() == 4) {//用户取消申请
                ivState.setImageResource(R.drawable.icon_sale_after_quxiao);
                tvShengTime.setTextColor(getResources().getColor(R.color.color_333333));
            } else {//退款成功
                ivState.setImageResource(R.drawable.icon_sale_after_chenggong);
                tvShengTime.setTextColor(getResources().getColor(R.color.color_333333));
            }
            tvState.setText(bean.getData().getStatus().getTitle().getMsg());
            tvShengTime.setText(bean.getData().getStatus().getTitle().getRemaining_time());
            tvApplyTime.setText("申请时间：" + bean.getData().getApply().getCreated_at());
            tvRefundMoney.setText("¥" + bean.getData().getApply().getApply_price());
            tvSaleAfterWhy.setText("售后原因：" + bean.getData().getApply().getReason());
            tvRefundShuoming.setText("退款说明：" + bean.getData().getApply().getDesc());
            LinearLayoutManager manager = new LinearLayoutManager(this);
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
            rvWhyImage.setLayoutManager(manager);
            ShopInOrderAfterSalesImageAdapter shopInOrderAfterSalesImageAdapter = new ShopInOrderAfterSalesImageAdapter(this, bean.getData().getApply().getImgs());
            rvWhyImage.setAdapter(shopInOrderAfterSalesImageAdapter);
            shopInOrderAfterSalesImageAdapter.setOnItemClickListener(new ItemClickListener() {
                @Override
                public void onItemClick(int i) {
                    Intent intent = new Intent(SalePiFaAfterDetailsActivity.this, BigImageViewActivity.class);
                    intent.putStringArrayListExtra("imgs", (ArrayList<String>) bean.getData().getApply().getImgs());
                    intent.putExtra("position", i);
                    startActivity(intent);
                }
            });
            addressCopy = bean.getData().getUser().getAddress();
            userPhone = bean.getData().getUser().getUser_phone();
            tvName.setText(bean.getData().getUser().getUser_name() + "    " + bean.getData().getUser().getUser_phone());
            tvAddress.setText("地址：" + bean.getData().getUser().getAddress());

            tvUserName.setText(bean.getData().getUser().getUser_name());
            roundedCorners = new RoundedCorners(2);
            //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
            options = RequestOptions.bitmapTransform(roundedCorners);
            options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                    .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                    .error(R.drawable.icon_defult);
            Glide.with(this).load(bean.getData().getUser().getAvatar()).apply(options.transform(new CircleCrop(), roundedCorners)).into(ivUser);
            if (bean.getData().getApply().getStatus() == 1) {
                tvUserState.setText("等待平台审核");
            } else if (bean.getData().getApply().getStatus() == 2) {
                tvUserState.setText("平台审核通过");
            } else if (bean.getData().getApply().getStatus() == 3) {
                tvUserState.setText("平台审核驳回");
            } else if (bean.getData().getApply().getStatus() == 4) {
                tvUserState.setText("用户已取消");
            } else if (bean.getData().getApply().getStatus() == 5) {
                tvUserState.setText("等待平台审核");
            } else if (bean.getData().getApply().getStatus() == 6) {
                tvUserState.setText("退款成功");
            }
            orderNum=bean.getData().getOrder().getOrder_id();
            tvOrderNum.setText(bean.getData().getOrder().getOrder_id());
            tvXiadanTime.setText(bean.getData().getOrder().getPay_time());
            tvPayType.setText(bean.getData().getOrder().getPay_type());
            tvPeisongType.setText(bean.getData().getOrder().getDelivery_name());

            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            rvGoods.setLayoutManager(linearLayoutManager);
            rvGoods.setItemAnimator(new DefaultItemAnimator());
            rvGoods.setNestedScrollingEnabled(false);
            OrderXfdSaleAfterGoodsAdapter   itemOrderXfdGoodsAdapter = new OrderXfdSaleAfterGoodsAdapter(this);
            itemOrderXfdGoodsAdapter.setList(bean.getData().getGoods());
            itemOrderXfdGoodsAdapter.setOnItemClickListener(new ItemClickListener() {
                @Override
                public void onItemClick(int position) {

                }
            });
            rvGoods.setAdapter(itemOrderXfdGoodsAdapter);

        } else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    private void initHigh() {
        item_bar = (RelativeLayout.LayoutParams) llState.getLayoutParams();
        item_bar.height = StatusBarUtil.getStatusBarHeight(this);
        llState.setLayoutParams(item_bar);
    }


    @OnClick({R.id.iv_back, R.id.tv_address_copy,R.id.tv_order_copy, R.id.tv_phone})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_address_copy:
                Utils.copy(this, addressCopy);
                Utils.toastMessage(this,"复制成功");
                break;
            case R.id.tv_order_copy:
                Utils.copy(this, orderNum);
                Utils.toastMessage(this,"复制成功");
                break;
            case R.id.tv_phone:
                if (EmptyUtil.isNotEmpty(userPhone)) {
                    callPhone(userPhone);
                }
                break;
        }
    }

    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    public void callPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }
}
